<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';

const router = useRouter();
const userStore = useUserStore();

const registerForm = reactive({
  userAccount: '',
  userPassword: '',
  confirmPassword: ''
});

const loading = ref(false);
const errorMessage = ref('');

const handleRegister = async () => {
  // 表单验证
  if (!registerForm.userAccount || !registerForm.userPassword || !registerForm.confirmPassword) {
    errorMessage.value = '请填写所有必填字段';
    return;
  }
  
  if (registerForm.userPassword !== registerForm.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致';
    return;
  }
  
  if (registerForm.userPassword.length < 8) {
    errorMessage.value = '密码长度不能少于8位';
    return;
  }
  
  loading.value = true;
  errorMessage.value = '';
  
  try {
    const success = await userStore.register(
      registerForm.userAccount,
      registerForm.userPassword,
      registerForm.confirmPassword
    );
    
    if (success) {
      // 注册成功后跳转到登录页
      router.push('/login');
    } else {
      errorMessage.value = userStore.error || '注册失败，请稍后再试';
    }
  } catch (error) {
    console.error('Registration error:', error);
    errorMessage.value = '注册过程中发生错误，请稍后再试';
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <div class="register-container">
    <div class="register-card">
      <h1 class="register-title">注册账号</h1>
      
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="userAccount">账号</label>
          <input 
            type="text" 
            id="userAccount" 
            v-model="registerForm.userAccount" 
            placeholder="请输入账号"
            autocomplete="username"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="userPassword">密码</label>
          <input 
            type="password" 
            id="userPassword" 
            v-model="registerForm.userPassword" 
            placeholder="请输入密码（至少8位）"
            autocomplete="new-password"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input 
            type="password" 
            id="confirmPassword" 
            v-model="registerForm.confirmPassword" 
            placeholder="请再次输入密码"
            autocomplete="new-password"
            required
          />
        </div>
        
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        
        <button type="submit" class="register-button" :disabled="loading">
          {{ loading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <div class="register-footer">
        <span>已有账号？</span>
        <router-link to="/login">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh - 120px); /* 减去header和footer的高度 */
  background-image: url('@/assets/background.svg');
  background-size: cover;
  background-position: center;
  padding: 0px;
  position: relative;
  overflow: hidden;
}

.register-card {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.register-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.2);
}

.register-title {
  font-size: 2rem;
  font-weight: 700;
  color: #3b82f6;
  margin-bottom: 1.5rem;
  text-align: center;
  position: relative;
}

.register-title::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: linear-gradient(90deg, #60a5fa, #3b82f6);
  border-radius: 3px;
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 0.875rem; /* 比登录页更小的间距 */
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem; /* 减小标签和输入框之间的间距 */
}

.form-group label {
  font-size: 0.875rem;
  font-weight: 500;
  color: #4b5563;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background-color: rgba(255, 255, 255, 0.8);
}

.form-group input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
  background-color: rgba(255, 255, 255, 0.95);
  transform: translateY(-2px);
}

.error-message {
  color: #ef4444;
  font-size: 0.875rem;
  margin-bottom: 0.25rem; /* 减小错误消息的底部间距 */
}

.register-button {
  padding: 0.75rem;
  background: linear-gradient(90deg, #3b82f6, #2563eb);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 6px rgba(37, 99, 235, 0.2);
  position: relative;
  overflow: hidden;
}

.register-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.7s ease;
}

.register-button:hover {
  background: linear-gradient(90deg, #2563eb, #1d4ed8);
  transform: translateY(-2px);
  box-shadow: 0 6px 10px rgba(37, 99, 235, 0.3);
}

.register-button:hover::before {
  left: 100%;
}

.register-button:disabled {
  background-color: #9ca3af;
  cursor: not-allowed;
}

.register-footer {
  margin-top: 1.5rem;
  text-align: center;
  font-size: 0.875rem;
  color: #6b7280;
}

.register-footer a {
  color: #3b82f6;
  text-decoration: none;
  font-weight: 500;
  position: relative;
  transition: all 0.3s ease;
}

.register-footer a::after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: -2px;
  left: 0;
  background-color: #3b82f6;
  transition: width 0.3s ease;
}

.register-footer a:hover {
  color: #2563eb;
}

.register-footer a:hover::after {
  width: 100%;
}
</style>